import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:razer_one/app/main/constants/color_constants.dart';

class MainTextField extends StatefulWidget {
  final String hintText; //输入文字
  final TextEditingController controller; //控制器
  final double? width; //宽度
  final double? height; //高度
  final ValueChanged? handle; //回调
  final bool? isPasswod; //是否是密码输入框
  final bool? isEdit;
  final TextInputType keyboardType;
  final List<TextInputFormatter>? inputFormatters;
  final int? maxLength;
  final int? maxLines;
  final double fontSize;
  final bool isShowcounterText;
  final bool isShowCleanIcon;
  const MainTextField(this.hintText, this.controller,
      {Key? key,
      this.width = double.infinity,
      this.height = 40,
      this.handle,
      this.isPasswod = false,
      this.isEdit = true,
      this.keyboardType = TextInputType.text,
      this.fontSize = 15,
      this.inputFormatters,
      this.maxLength,
      this.maxLines = 1,
      this.isShowcounterText = false,
      this.isShowCleanIcon = true})
      : super(key: key);

  @override
  _MainTextFieldState createState() => _MainTextFieldState();
}

class _MainTextFieldState extends State<MainTextField> {
  bool _isShowCleanIcon = false;
  final _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    //获取是否成为响应者
    _focusNode.addListener(() {
      setState(() {
        _isShowCleanIcon =
            _focusNode.hasFocus && widget.controller.text.isNotEmpty;
      });
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      // color: Colors.red,
      width: widget.width,
      height: widget.height,
      child: TextField(
        focusNode: _focusNode,
        enabled: widget.isEdit,
        controller: widget.controller,
        obscureText: widget.isPasswod!,
        keyboardType: widget.keyboardType,
        inputFormatters: widget.inputFormatters,
        style: TextStyle(fontSize: widget.fontSize),
        maxLength: widget.maxLength,
        maxLines: widget.maxLines, //设置为null,高度会随着文本变化,如果指定比如为7,高度会给个固定
        decoration: InputDecoration(
          counterText: widget.isShowcounterText ? null : "", //去掉最大多少个子显示
          // errorText: "错误",
          //去掉下划线
          // border: InputBorder.none,
          border: const OutlineInputBorder(borderSide: BorderSide.none),
          //同时设定
          filled: true,
          fillColor: const Color(kTextFBackGroundColor),
          suffixIcon: Visibility(
            visible: _isShowCleanIcon,
            maintainState: true,
            child: IconButton(
                onPressed: () {
                  widget.controller.text = "";
                  setState(() {
                    _isShowCleanIcon = false;
                  });
                },
                icon: const Icon(
                  Icons.cancel,
                  color: Colors.grey,
                  size: 20,
                )),
          ),
          labelStyle: const TextStyle(color: Color(kplaceholderColor)),
          contentPadding: const EdgeInsets.only(left: 8, right: 8, top: 8),
          hintText: widget.isEdit! ? widget.hintText : null,
          // labelText: widget.isEdit! ? widget.placeText : null,
          // border: OutlineInputBorder(),
          // filled: true,
          // fillColor: Colors.green,
        ),
        //onEditingComplete

        onSubmitted: (value) {},
        onChanged: (String value) {
          setState(() {
            _isShowCleanIcon = value.isNotEmpty;
          });
          if (widget.handle != null) {
            widget.handle!(value);
          }
        },
      ),
    );
  }
}
